package com.hzp.lolnews.activity.view;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.hzp.lolnews.R;
import com.hzp.lolnews.activity.model.Imain;
import com.hzp.lolnews.base.BaseActivity;
import com.hzp.lolnews.utils.FragmentFactory;

public class MainActivity extends BaseActivity implements Imain, RadioGroup.OnCheckedChangeListener {
    private Toolbar mToolbar;
    private RadioGroup mRdg;
    private RelativeLayout mRlayout;

    @Override
    protected void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar_tb);
        mRdg = (RadioGroup) findViewById(R.id.rdg_zhidemai);
        mRlayout = (RelativeLayout) findViewById(R.id.container_rl);
        mRdg.setOnCheckedChangeListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setOverflowIcon(getDrawable(R.mipmap.ic_action_search));
        }
        creatFragment(0);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void creatFragment(int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = FragmentFactory.createFragment(i);
        transaction.replace(R.id.container_rl, fragment);
        transaction.commit();
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            if (radioGroup.getChildAt(i).getId() == checkId) {
                //处理点击监听
                creatFragment(i);
            }
        }
    }
}
